﻿package mortal.game.manager.msgTip
{
    import com.gengine.global.*;
    import com.gengine.utils.pools.*;
    import flash.display.*;
    import flash.utils.*;
    import mortal.game.manager.*;

    public class MsgRollTipsBmpImpl extends Sprite implements IMsgRollTipsImpl
    {
        private var _showArr:Array;
        private var _showDict:Dictionary;
        private var _showMax:int = 6;
        private var _canvas:DisplayObjectContainer;

        public function MsgRollTipsBmpImpl()
        {
            this._showArr = [];
            this._showDict = new Dictionary();
            this.mouseChildren = false;
            this.mouseEnabled = false;
            return;
        }// end function

        private function itemHideCallBack(param1:RollTipsBmpItem) : void
        {
            if (this._showArr.length == 1)
            {
                this.removeMsgImpl(param1);
                this.hide();
            }
            else
            {
                this.updateLayOut(param1, false);
            }
            return;
        }// end function

        private function removeMsgImpl(param1:RollTipsBmpItem) : void
        {
            if (this._showDict[param1.msg])
            {
                delete this._showDict[param1.msg];
            }
            this.removeChild(param1);
            param1.dispose();
            ObjectPool.disposeObject(param1, RollTipsBmpItem);
            var _loc_2:* = this._showArr.indexOf(param1);
            if (_loc_2 != -1)
            {
                this._showArr.splice(_loc_2, 1);
            }
            return;
        }// end function

        private function showMsgImpl(param1:RollTipsBmpItem) : void
        {
            if (!this.parent)
            {
                if (!this._canvas)
                {
                    this._canvas = LayerManager.msgTipLayer;
                }
                this._canvas.addChild(this);
                this.stageResize();
            }
            this.updateLayOut(param1, true);
            return;
        }// end function

        private function updateLayOut(param1:RollTipsBmpItem, param2:Boolean) : void
        {
            var _loc_5:RollTipsBmpItem = null;
            var _loc_6:RollTipsBmpItem = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (param2)
            {
                if (this._showArr.length >= this._showMax)
                {
                    _loc_6 = this._showArr.pop();
                    this.removeMsgImpl(_loc_6);
                }
                this._showArr.unshift(param1);
                param1.y = this.getPosMaxY();
                this.addChild(param1);
                _loc_3 = this._showArr.length;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_5 = this._showArr[_loc_4];
                    _loc_5.moveTo(this.getPosY(this._showMax - _loc_4 - 1), true);
                    _loc_4++;
                }
            }
            else
            {
                _loc_4 = this._showArr.indexOf(param1);
                _loc_3 = this._showArr.length;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_5 = this._showArr[_loc_4];
                    _loc_5.moveTo(this.getPosY(this._showMax - _loc_4), true);
                    _loc_4++;
                }
                this.removeMsgImpl(param1);
            }
            return;
        }// end function

        private function getPosY(param1:int) : int
        {
            return param1 * 37 - 40;
        }// end function

        private function getPosMaxY() : int
        {
            return this._showMax * 37;
        }// end function

        public function get msgLength() : int
        {
            return this._showArr.length;
        }// end function

        public function showMsg(param1:String, param2:MsgRollTipsType, param3:Boolean = false) : void
        {
            var _loc_4:* = ObjectPool.getObject(RollTipsBmpItem);
            if (param3)
            {
                if (this._showDict[param1])
                {
                    return;
                }
                this._showDict[param1] = _loc_4;
            }
            _loc_4.updateData(param1, param2, this.itemHideCallBack);
            this.showMsgImpl(_loc_4);
            return;
        }// end function

        public function stageResize() : void
        {
            if (this.parent)
            {
                x = Global.stage.stageWidth / 2 + 200;
                y = Global.stage.stageHeight / 2;
            }
            return;
        }// end function

        public function get canvas() : DisplayObjectContainer
        {
            return this._canvas;
        }// end function

        public function set canvas(param1:DisplayObjectContainer) : void
        {
            this._canvas = param1;
            return;
        }// end function

        public function hide() : void
        {
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            return;
        }// end function

    }
}
